<?php
/**
 * Created by PhpStorm.
 * User: mf
 * Date: 2017/9/4
 * Time: 16:43
 */
namespace app\admin\logic;

use think\Db;
use tool\Common;

class Seller extends Base
{
    //得到商家列表(status-1审核未通过1正在审核中2审核通过)
    public function getSellerList()
    {
        $roleID = $this->getRoleID();
        if ($roleID == 5) {
            $this->data['status'] = 2;
        }
        /*$amount = Db::name('user')->where('roleID',1)->where('status',$this->data['status'])->count();
        if (!$amount) {
            return Common::rm(-2,'数据为空');
        }*/
        $allSellerList = Db::name('user')->where('roleID', 1)->where('status', $this->data['status'])
            ->page($this->data['page'], $this->data['count'])
            ->field('id,sellerName')
            ->order('addtime desc')->select();
        $ar = [];
        foreach ($allSellerList as $key => $val) {
            $ar[$key]['sellerID'] = $val['id'];
            $ar[$key]['companyName'] = $val['sellerName'];
        }
        return Common::rm(1, '操作成功', [
            /*'totalNumber' => $amount,*/
            'sellerList' => $ar
        ]);
    }

    //谦程网络通过审核按钮
    public function success()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 3) {
            return Common::rm(-2, '您没有权限操作');
        }
        if (isset($this->data['sellerID']) && $this->data['sellerID']) {
            $sellerID = (int)$this->data['sellerID'];
            $seller = Db::name('user')->where('id', $sellerID)->find();
            if ($seller) {
                Db::name('user')->where('id', $sellerID)->update(['status' => 2]);
                return Common::rm(1, '操作成功');
            } else {
                return Common::rm(-2, '操作失败');
            }
        } else {
            return Common::rm(-2, '请选择商家');
        }
    }

    //谦程网络拒绝按钮
    public function failure()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 3) {
            return Common::rm(-2, '您没有权限操作');
        }
        if (isset($this->data['sellerID']) && $this->data['sellerID']) {
            $sellerID = (int)$this->data['sellerID'];
            $seller = Db::name('user')->where('id', $sellerID)->find();
            if ($seller) {
                Db::name('user')->where('id', $sellerID)->update(['status' => -1]);
                return Common::rm(1, '操作成功');
            } else {
                return Common::rm(-2, '操作失败');
            }
        } else {
            return Common::rm(-2, '请选择商家');
        }
    }

    //查看未通过、已通过商家审核意见
    public function getSellerCheckMsg()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 3) {
            return Common::rm(-2, '您没有权限操作');
        }
        if (isset($this->data['sellerID']) && $this->data['sellerID']) {
            $checkMsg = Db::name('user')->where('id', $this->data['sellerID'])->value('checkMsg');
            return Common::rm(1,'操作成功',[
                'checkMsg' => $checkMsg
            ]);
        } else {
            return Common::rm(-2, '请选择需要查看的商家');
        }
    }

    //总队添加供应商按钮
    public function addSupplier()
    {
        $roleID = $this->getRoleID();
        $userID = $this->getUserID();
        if ($roleID != 5) {
            return Common::rm(-2,'您没有权限操作');
        }
        if (!is_array($this->data['sellerID'])) {
            return Common::rm(-2,'操作失败');
        }
        $sellerIDs = Db::name('team_select')->where('teamID',$userID)->where('sellerID','in',$this->data['sellerID'])->column('sellerID');
        if ($sellerIDs) {
            $this->data['sellerID'] = array_diff($this->data['sellerID'],$sellerIDs);
            if (!$this->data['sellerID']) {
                return Common::rm(-2,'您添加的商家已经成为供应商');
            }
        }
        $ar = [];
        foreach ($this->data['sellerID'] as $key => $val) {
            $ar[] = [
                'teamID' => $userID,
                'sellerID' => $val,
                'addtime' => THINK_START_TIME
            ];
        }
        $res = Db::name('team_select')->insertAll($ar);
        Db::name('user')->where('id','in',$this->data['sellerID'])->update(['roleID' => 2]);
        if (!$res) {
            return Common::rm(-2,'供应商添加失败');
        }
        return Common::rm(1,'供应商添加成功');
    }

    //总队删除供应商按钮
    public function deleteSuppliers()
    {
        $roleID = $this->getRoleID();
        $userID = $this->getUserID();
        if ($roleID != 5) {
            return Common::rm(-2,'您没有权限操作');
        }
        if (!is_array($this->data['sellerID'])) {
            return Common::rm(-2,'请选择需要删除的供应商');
        }
        $sellerIDs = Db::name('team_select')->where('sellerID','neq',$userID)->where('id','in',$this->data['sellerID'])->column('sellerID');
        if (!$sellerIDs) {
            Db::name('user')->where('id','in',$this->data['sellerID'])->update(['roleID' => 1]);
        }
        Db::name('team_select')->where('teamID',$userID)->where('sellerID','in',$this->data['sellerID'])->delete();
        return Common::rm(1,'删除成功');
    }

    //总队得到添加的供应商列表
    public function getSupplierList()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 5) {
            return Common::rm(-2,'您没有权限操作');
        }
        $userID = $this->getUserID();
        $sellerList = Db::view('team_select','addtime')
            ->view('user','id,sellerName','user.id = team_select.sellerID')
            ->where('teamID',$userID)->order('addtime')->select();
        if (!$sellerList) {
            return Common::rm(-3,'数据为空');
        }
        $ar = [];
        foreach ($sellerList as $key => $val) {
            $ar[$key]['sellID'] = $val['id'];
            $ar[$key]['sellerName'] = $val['sellerName'];
            $ar[$key]['addtime'] = date('Y-m-d H:i:s',$val['addtime']);
        }
        return Common::rm(1,'操作成功',[
            'supplierList' => $ar
        ]);
    }

    //得到供应商下可添加产品
    public function getAddProductList()
    {
        if (!isset($this->data['sellerID']) || !$this->data['sellerID']) {
            return Common::rm(-2,'请选择商家');
        }
        $productList = Db::view('product','id,name,model,price,quality,manufacturer')
            ->view('product_content','titleImage','product_content.productID = product.id')
            ->where('product.sellerID',$this->data['sellerID'])
            ->select();
        if (!$productList) {
            return Common::rm(-3,'商家暂未上传产品');
        }
        foreach ($productList as $key => $val) {
            $productList[$key]['productID'] = $val['id'];
            unset($productList[$key]['id']);
            $productList[$key]['titleImage'] = json_decode($val['titleImage'],true)[0];
        }
        return Common::rm(1,'操作成功',[
            'productList' => $productList
        ]);
    }

    //得到供应商下可购买产品
    public function getProductList()
    {
        if (!isset($this->data['sellerID']) || !$this->data['sellerID']) {
            return Common::rm(-2,'请选择商家');
        }
        $userID = $this->getUserID();
        $selectID = Db::name('team_select')->where('teamID',$userID)->where('sellerID',$this->data['sellerID'])->value('id');
        if (!$selectID) {
            return Common::rm(-2,'操作失败');
        }
        $productList = Db::view('team_select_item','productID')
            ->view('product','name,model,price,quality,manufacturer','product.id = team_select_item.productID')
            ->view('product_content','titleImage','product_content.productID = product.id')
            ->where('selectID',$selectID)
            ->select();
        if (!$productList) {
            return Common::rm(-3,'商家暂未上传产品');
        }
        foreach ($productList as $key => $val) {
            $productList[$key]['titleImage'] = json_decode($val['titleImage'],true)[0];
        }
        return Common::rm(1,'操作成功',[
            'productList' => $productList
        ]);
    }

    //总队添加产品到支队购买库
    public function addProductToBuy()
    {
        $roleID = $this->getRoleID();
        $teamID = $this->getUserID();
        if ($roleID != 5) {
            return Common::rm(-2,'对不起，您没有操作权限');
        }
        if (!isset($this->data['sellerID']) || !$this->data['sellerID']) {
            return Common::rm(-2,'请选择供应商');
        }
        if (!isset($this->data['productID']) || !$this->data['productID']) {
            return Common::rm(-2,'请选择商品');
        }
        $dbSeller = Db::name('team_select')->where('teamID',$teamID)->where('sellerID',$this->data['sellerID'])->find();
        if (!$dbSeller) {
            return Common::rm(-2,'对不起，您选择的商家不是供应商');
        }
        $productIDs = Db::name('team_select_item')->where('selectID',$dbSeller['id'])->column('productID');
        if ($productIDs) {
            foreach ($this->data['productID'] as $key => $val) {
                if (in_array($val,$productIDs)) {
                    unset($this->data['productID'][$key]);
                }
            }
        }
        if (!$this->data['productID']) {
            return Common::rm(-2,'商品已经添加到产品库');
        }
        $ar= [];
        foreach ($this->data['productID'] as $key => $val) {
            $ar[] = [
                'selectID' => $dbSeller['id'],
                'productID' => $val,
                'addtime' => THINK_START_TIME
            ];
        }
        $res = Db::name('team_select_item')->insertAll($ar);
        if ($res) {
            return Common::rm(1,'商品添加成功');
        } else {
            return Common::rm(-2,'商品添加失败');
        }
     }

    //供应商得到需要议价的订单列表
    public function getNeedbargainCartList()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 2) {
            return Common::rm(-2,'对不起，您没有操作权限');
        }
        $userID = $this->getUserID();
        $cartList = Db::view('shopping_cart','id,teamID,number,price,total,productID')
            ->view('user','sellerName','user.id = shopping_cart.teamID')
            ->view('product','name,model','product.id = shopping_cart.productID')
            ->view('product_content','titleImage','product_content.productID = product.id')
            ->where('shopping_cart.sellerID',$userID)
            ->where('shopping_cart.status',1)
            ->order('shopping_cart.addtime desc')
            ->select();
        if (!$cartList) {
            return Common::rm(-2,'数据为空');
        }
        $ar = [];
        foreach ($cartList as $key => $val) {
            $ar[$val['teamID']][] = $val;
        }
        $arr = [];
        foreach ($ar as $k => $v) {
            $arr[$k]['teamName'] = $v[0]['sellerName'];
            foreach ($v as $_k => $_v) {
                $arr[$k]['cart'][$_k]['cartID'] = $_v['id'];
                $arr[$k]['cart'][$_k]['productID'] = $_v['productID'];
                $arr[$k]['cart'][$_k]['productName'] = $_v['name'];
                $arr[$k]['cart'][$_k]['model'] = $_v['model'];
                $arr[$k]['cart'][$_k]['number'] = $_v['number'];
                $arr[$k]['cart'][$_k]['price'] = $_v['price'];
                $arr[$k]['cart'][$_k]['totalPrice'] = $_v['total'];
                $arr[$k]['cart'][$_k]['titleImage'] = json_decode($_v['titleImage'], true)[0];
            }
        }
        $arr = array_reverse($arr);
        return Common::rm(1,'操作成功',[
            'needbargainCartList' => $arr
        ]);
    }
}